using System;
using System.Web.Configuration;

namespace WinnerCMS.Common.Utilities
{
	public sealed class WebConfigHelper
	{
		public static string GetConfigString(string key)
		{
			string _key = "AppSettings-" + key;
		    object obj;
            if (!CacheHelper.TryGet(_key,out obj) )
			{
				try
				{
					var configuration = WebConfigurationManager.OpenWebConfiguration("~");
					if (0 < configuration.AppSettings.Settings.Count && configuration.AppSettings.Settings[key] != null)
					{
						obj = configuration.AppSettings.Settings[key].Value;
                        CacheHelper.Add(_key, obj, 180);
					}
				}
				catch
				{
				}
			}
			return (obj != null) ? obj.ToString() : null;
		}
		public static bool GetConfigBool(string key)
		{
			bool result = false;
			string configString = WebConfigHelper.GetConfigString(key);
			if (!string.IsNullOrWhiteSpace(configString))
			{
				try
				{
					result = bool.Parse(configString);
				}
				catch (FormatException)
				{
				}
			}
			return result;
		}
		public static decimal GetConfigDecimal(string key)
		{
			decimal result = 0m;
			string configString = WebConfigHelper.GetConfigString(key);
			if (!string.IsNullOrWhiteSpace(configString))
			{
				try
				{
					result = decimal.Parse(configString);
				}
				catch (FormatException)
				{
				}
			}
			return result;
		}
		public static int GetConfigInt(string key)
		{
			int result = 0;
			string configString = WebConfigHelper.GetConfigString(key);
			if (!string.IsNullOrWhiteSpace(configString))
			{
				try
				{
					result = int.Parse(configString);
				}
				catch (FormatException)
				{
				}
			}
			return result;
		}
	}
}
